import { tt } from "../../ttapi_interface/ttapi.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js";
import { EUI_Style } from "./EUI_Style.js";
import { EUI_Window } from "./EUI_Window.js";

export class EUI_Dialog_Input extends tt2.QUI_Container {
    constructor() {
        super();
        this.localRect.setAsFill();

        //Back
        let back = EUI_Style.CreateWhiteImage();
        back.color = new tt.Color(0, 0, 0, 0.5);
        this.addChild(back);


        let border = EUI_Style.CreateDefaultBack();
        border.localRect.setHPosFill(200, 200);
        border.localRect.setVPosFill(200, 200);
        this.addChild(border);

        let overlay = new tt2.QUI_Overlay();
        overlay.localRect.setAsFill();
        border.addChild(overlay);
        //标签
        this._label = new tt2.QUI_Label(EUI_Style.font);
        this._label.localRect.setHPosByCenter(200);
        this._label.localRect.setVPosByTopBorder(20, 20);
        border.addChild(this._label);

        
        this._input = new tt2.QUI_TextBox_Prompt(EUI_Style.font, "");
        this._input.border =EUI_Style.CreateDefaultBorder();
        this._input.localRect.setHPosByCenter(200);
        this._input.localRect.setVPosByTopBorder(20, 50);
        border.addChild(this._input);
        {
            //OK按钮
            let btn = EUI_Style.CreateButton("确定");
            btn.localRect.setHPosByLeftBorder(80,50);
            btn.localRect.setVPosByCenter(16);
            btn.localRect.offsetY1 = 20;
            btn.localRect.offsetY2 = 36;
            border.addChild(btn);
            btn.OnClick = () => {
                if (this._win.GetPopTag() == this._tag) {
                    this._yes = true;
                    this._isShow = false;
                    this._win.PopModel();
                }
            }
        }

        {
            //No按钮
            let btn = EUI_Style.CreateButton("取消");
            btn.localRect.setHPosByRightBorder(80, 50);
            btn.localRect.setVPosByCenter(16);
            btn.localRect.offsetY1 = 20;
            btn.localRect.offsetY2 = 36;
            border.addChild(btn);
            btn.OnClick = () => {
                if (this._win.GetPopTag() == this._tag) {
                    this._yes = false;
                    this._isShow = false;
                    this._win.PopModel();
                }
            }
        }
    }
    private _InitMsg(msg: string, maxlen: number) {
        this._label.text = msg;
        this._input.text = "";
        this._input.maxlen = maxlen;
        this._yes = false;
    }
    private _input: tt2.QUI_TextBox_Prompt;

    private _label: tt2.QUI_Label;
    private _isShow: boolean;
    private _win: EUI_Window
    private _yes: boolean;
    readonly _tag: string = "_dialog_msgbox_";
    async Show(win: EUI_Window, msg: string, maxlen: number): Promise<{ result: boolean, text: string }> {
        this._isShow = true;
        this._win = win;
        this._InitMsg(msg, maxlen);

        win.PushModel(this, this._tag, () => {
            if (this._win.GetPopTag() == this._tag) {
                this._isShow = false;
                this._win.PopModel();
            }
        });

        while (this._isShow) {
            await tt.sleep(1);
        }
        return { text: this._input.text, result: this._yes };
    }

}